package com.yh.rent.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yh.rent.biz.HouseBiz;
import com.yh.rent.biz.impl.HouseBizImpl;
import com.yh.rent.biz.impl.StreetBizImpl;
import com.yh.rent.biz.impl.TypeBizImpl;
import com.yh.rent.entity.Condition;
import com.yh.rent.entity.House;
import com.yh.rent.entity.Page;
import com.yh.rent.entity.Street;
import com.yh.rent.entity.Type;
import com.yh.rent.utils.ToKnowStringEncoding;

/**
 * Servlet implementation class HouseServlet
 */
public class HouseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HouseServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path
				+ "/";

		int pageIndex = 1, pageSize = 4;
		HouseBiz hbiz = new HouseBizImpl();
		Condition condition = (Condition)request.getAttribute("condition") ; 
		if (condition == null) {
			condition = new Condition();
		}
		
		String pageIndexStr = request.getParameter("index") ;
		if (pageIndexStr != null && ! "".equals(pageIndexStr)) {
			pageIndex = Integer.valueOf(pageIndexStr) ;
		}
		String titleStr = request.getParameter("title");
		System.out.println(ToKnowStringEncoding.getEncoding(titleStr));
		String titleStr2 = null ;
		if (titleStr != null &&  "ISO-8859-1".equals(ToKnowStringEncoding.getEncoding(titleStr))) {
			titleStr = new String(titleStr.getBytes("ISO-8859-1"),"utf-8") ;
		}
		System.out.println(ToKnowStringEncoding.getEncoding(titleStr));
		System.out.println(titleStr);
		String price = request.getParameter("price");
		String minPrice = null , maxPrice = null ;
		if (price != null && ! "".equals(price)) {
			int k = price.indexOf("-");
			minPrice = price.substring(0, k);
			maxPrice = price.substring(k + 1);
		}

		String streetId = request.getParameter("street_id");
		String typeId = request.getParameter("type_id");

		String floorage = request.getParameter("floorage");
		String minFloorage = null , maxFloorage = null ;
		if (floorage != null && ! "".equals(floorage)) {
			int m = floorage.indexOf("-");
			 minFloorage = floorage.substring(0, m);
			 maxFloorage = floorage.substring(m + 1);
		}
		
		if (titleStr != null && ! "".equals(titleStr)) {
			condition.setTitleStr(titleStr);
			
		}
		if (maxPrice != null && ! "".equals(maxPrice)) {
			condition.setMaxPrice(Double.valueOf(maxPrice));
		}
		if (minPrice != null && ! "".equals(minPrice)) {
			condition.setMinPrice(Double.valueOf(minPrice));
		}
		if (minFloorage != null && ! "".equals(minFloorage)) {
			condition.setMinFloorage(Double.valueOf(minFloorage));
		}
		if (maxFloorage != null && ! "".equals(maxFloorage)) {
			condition.setMaxFloorage(Double.valueOf(maxFloorage));
		}
		if (typeId != null && ! "".equals(typeId)) {
			condition.setTypeId(Integer.valueOf(typeId));
		}
		if (streetId != null && ! "".equals(streetId)) {
			condition.setStreetId(Integer.valueOf(streetId));
		}
		//将所有条件加入一个map集合root中
		Map<String, Object> root = new HashMap<>() ;
		root.put("titleStr", titleStr) ;
		root.put("price", price) ;
		root.put("streetId", streetId) ;
		root.put("typeId", typeId) ;
		root.put("floorage", floorage) ;
		// 1.获取所有新闻信息，分页
		Page<House> hpages = hbiz.getHouseByCondition(pageIndex, pageSize, condition);
		List<House> hlist = hpages.getPageList();
		//获取所有道路
		List<Type> tlist = new TypeBizImpl().getAllTypes() ;
		List<Street> slist = new StreetBizImpl().getAllStreets() ;
		
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("hlist", hlist);
		request.setAttribute("slist", slist);
		request.setAttribute("tlist", tlist);
		request.setAttribute("totalPageCount", hpages.getTotalPageCount());
		request.setAttribute("basePath", basePath);
		request.setAttribute("condition", condition);
		request.setAttribute("root", root);
		request.getRequestDispatcher("/WEB-INF/templates/list.ftl").forward(request, response);
	}
}
